package com.ruoyi.train.controller;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.company.domain.Company;
import com.ruoyi.company.service.ICompanyServer;
import com.ruoyi.train.domain.Train;
import com.ruoyi.train.service.TrainService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公司培训记录Controller
 *
 * @author zjh
 * @date 2022-05-06
 */
@Controller
@RequestMapping("/train")
public class TrainController extends BaseController {
    private String prefix = "train";

    @Autowired
    private TrainService trainService;

    @Autowired
    private ICompanyServer companyServer;

    @RequiresPermissions("business:train:view")
    @GetMapping()
    public String train(ModelMap mmap) {
        if ("913701003534974709".equals(ShiroUtils.getSysUser().getUserName())) {
            mmap.put("code", 1);
        } else {
            mmap.put("code", 0);
        }

        return prefix + "/train";
    }

    /**
     * 查询公司培训记录列表
     */
    @RequiresPermissions("business:train:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(required = false, value = "beginTime") String beginTime,
                              @RequestParam(required = false, value = "endTime") String endTime) {
        Train train = new Train();
        train.setBeginTime(beginTime);
        train.setEndTime(endTime);
        startPage();
        Long companyId = SecurityUtils.getLoginUser().getCompanyId();
        train.setCompanyId(companyId);
        List<Train> list = trainService.selectTrainByCompanyId(train);
        return getDataTable(list);
    }

    /**
     * 导出公司培训记录列表
     */
    @RequiresPermissions("business:train:export")
    @Log(title = "公司培训记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public void export(Train train, HttpServletResponse response) {

        Long companyId = SecurityUtils.getLoginUser().getCompanyId();
        train.setCompanyId(companyId);
        List<Train> list = trainService.selectTrainByCompanyId(train);
        ExcelUtil<Train> util = new ExcelUtil<Train>(Train.class);
        util.exportExcel(response, list, "培训记录");
    }

    /**
     * 新增公司培训记录
     */
    @GetMapping("/selectInformation")
    public Map<String, Object> add(ModelMap mmap) {
        Map<String, Object> resMap = new HashMap<>();
        Date date = DateUtils.getNowDate();
        Long companyId = SecurityUtils.getLoginUser().getCompanyId();
        Company company = companyServer.selectByCompanyId(companyId);
        if (company != null) {

            resMap.put("cop", company.getCorporateRepresentative());
        }

        resMap.put("date", date);
        return resMap;
    }

    /**
     * 新增保存公司培训记录
     */
    @RequiresPermissions("business:train:add")
    @Log(title = "公司培训记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody Train train) {
        return toAjax(trainService.insertTrain(train));
    }

    /**
     * 修改公司培训记录
     */
    @GetMapping("/edit/{trainId}")
    public String edit(@PathVariable("trainId") Long trainId, ModelMap mmap) {
        Train train = trainService.selectTrainById(trainId);
        mmap.put("train", train);

        return prefix + "/edit";
    }

    /**
     * 修改保存公司培训记录
     */
    @RequiresPermissions("business:train:edit")
    @Log(title = "公司培训记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody Train train) {
        return toAjax(trainService.updateTrain(train));
    }

    /**
     * 删除公司培训记录
     */
    @RequiresPermissions("business:train:remove")
    @Log(title = "公司培训记录", businessType = BusinessType.DELETE)
    @GetMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@RequestParam(required = false,value = "trainId") String trainId) {
        return toAjax(trainService.deleteTrainByIds(trainId));
    }
}
